import React from 'react'
import {GlobalContext} from "../../context";
import {Route, Redirect} from "react-router-dom"


class AuthRoute extends React.Component {

    static contextType = GlobalContext

    render() {
        const {component: Component, ...rest} = this.props
        const auth = this.context.auth
        const login = auth.login
        return <Route
            {...rest}
            render={
                props =>
                    (
                        login
                            ? <Component {...props} />
                            : <Redirect to={{pathname: '/login', from: props.location}}/>
                    )
            }
        />
    }
}

export default AuthRoute
